package com.hxzy.util;

import com.alibaba.fastjson.JSON;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.R;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

/**
 * @author Administrator
 */
public class ServletReactUtils {

    public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String msg)
    {
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().add(HttpHeaders.CONTENT_TYPE,  MediaType.APPLICATION_JSON_VALUE);
        R<?> result = R.build(AckCode.FAIL);
        result.setMsg(msg);

        DataBuffer dataBuffer = response.bufferFactory().wrap(JSON.toJSONString(result).getBytes());
        return response.writeWith(Mono.just(dataBuffer));
    }
}
